package com.sgb.zhaocai.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.LeaseNeedListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.fragment.LeaseNeedFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/7/1
 * AUTHOR:F_Q
 * Description:
 */
public class LeaseNeedModel  extends ViewModel {
    //列表
    public MutableLiveData<List<LeaseNeedListEntity>> leaseneedlist = new MutableLiveData<>();
    //地址
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mAddressData = new MutableLiveData<>();
    //排序集合
    public MutableLiveData<List<CommonPopEntity>> SortTypeList = new MutableLiveData<>();
    //时间
    public MutableLiveData<List<CommonPopEntity>> UseTimeList = new MutableLiveData<>();
    public boolean isLastPage;

    /**
     * 机械列表
     * @param requestBean
     */
    public void LeaseNeedList(LeaseNeedFragment fragment, RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getLeaseNeedList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseNeedListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LeaseNeedListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                leaseneedlist.setValue(data.getData().getList());
            }
        });
    }

    /**
     * 设备列表
     * @param fragment
     * @param requestBean
     */
    public void LeaseNeedList2(LeaseNeedFragment fragment, RequestBean requestBean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getEquipmentList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseNeedListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LeaseNeedListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                leaseneedlist.setValue(data.getData().getList());
            }
        });
    }

    /**
     * 地址详情
     */
    public void getAddressModel(LeaseNeedFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    mAddressData.setValue(data.getData());
                }
            }
        });
    }

    /**
     * 排序
     */
    public void getSortType() {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("1", "综合排序"));
        datas.add(new CommonPopEntity("3", "使用时间↓"));
        datas.add(new CommonPopEntity("2", "使用时间↑"));
        datas.add(new CommonPopEntity("4", "距离↓"));
        SortTypeList.setValue(datas);
    }

    /**
     * 使用时间
     * @param fragment
     */
    public void getUseTime(LeaseNeedFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getfindProjectTimeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    UseTimeList.setValue(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }


}
